package home;

import android.content.Context;
import android.content.SharedPreferences;
import android.drm.ProcessedData;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Response;
import com.google.gson.Gson;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import base.BasePage;
import base.SharedPreferencesutils;
import bean.News;
import httptool.QLParser;
import httptool.getHttpUrl;
import httptool.takeUrlResource;
import news.fragment.MenuFragment2;
import shanshuitiancheng.news.MainActivity;

/**
 * Created by cch on 2016/6/14.
 */

public class NewsCenterPage extends BasePage {
    public NewsCenterPage(Context ct){
        super(ct);
    }

    @Override
    public View initView(LayoutInflater inflater) {
//        TextView textView = new TextView(ct);
       TextView textView =  new TextView(ct);
        textView.setText("我是新聞中心");
        return textView;
    }

    @Override
    public void initData() {
        String value = SharedPreferencesutils.getString(ct,getHttpUrl.index);
        if (!TextUtils.isEmpty(value)){
            TestGet();
        }

    }

/*
* 1 没有网络的时候，可以提高体验，因为如果不缓存数据的话，会变成空白，这样用户体验不好
* 2 有可能  如果能联网的话，我们需要更新数据
* 3 在开发的过程中 首先 必须从本地区找数据
* 然后再通过去跟服务器交互，从服务器拿取数据
* 然后显示。。。。
* */

    private List<String> menuNewCenterList = new ArrayList<String>();
    private void TestGet() {
    takeUrlResource.getJson(ct,getHttpUrl.index,new Response.Listener<String>() {
        @Override
        public void onResponse(String s) {
            News data = QLParser.parse(s, News.class);//s 为所传参数，news.class为你存放json数据的类
            List<News.NewsBean> news = data.getNews();//获取json里面的hot数据
            for (News.NewsBean hb:news){
                String title = hb.getTitle();
                menuNewCenterList.add(title);
//              menuNewCenterList.add(hb.getTitle());
//                SharedPreferences sp = ct.getSharedPreferences("config", 0);
//                SharedPreferences.Editor edit = sp.edit();
//                edit.putString("xxxxx", hb.getTitle());
//                edit.commit();
//                SharedPreferencesutils.saveString(ct,"xxxxx",hb.getTitle());
                SharedPreferencesutils.saveString(ct,getHttpUrl.index,s);
                Log.i("所有数据",news+"新闻内容");
            }
            MenuFragment2 menuFragment2 = ((MainActivity)ct).getMenuFragment();
            menuFragment2.initMenu(menuNewCenterList);
        }
    });
    }
}
